package service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import serviceInterface.HelperServiceInterface;
import util.DateUtil;
import domain.DeliveryType;
import domain.Driver;
import domain.Envelope;
import domain.EnvelopeType;
import domain.LogisticManager;
import domain.Mission;
import domain.MissionStatus;
import domain.POS;
import domain.Person;
import domain.Route;
import domain.Schedule;
import domain.ScheduleStatus;

@Stateless
public class HelperService implements HelperServiceInterface{

	@PersistenceContext
	private EntityManager entityManager;
	
	public void createTestSchedule1() throws ParseException{
		Person person = new Person();
			person.setFullName("Nattawat");
			person.setUserName("driver1");
			person.setPassword("driver1");
			try {
				person.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		Driver driver = new Driver();
			person.addRoles(driver);
		
		entityManager.persist(person);
		entityManager.flush();
		
		
		
		Person person2 = new Person();
		person2.setFullName("Bidur");
		person2.setUserName("driver2");
		person2.setPassword("driver2");
		try {
			person2.encodePassword();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	Driver driver2 = new Driver();
		person2.addRoles(driver2);
	
	entityManager.persist(person2);
	entityManager.flush();
	
	
	
	
		
		Person person1 = new Person();
			person1.setFullName("Kik");
			person1.setUserName("manager");
			person1.setPassword("manager");
			try {
				person1.encodePassword();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		LogisticManager logistic = new LogisticManager();
			person1.addRoles(logistic);
		
		entityManager.persist(person1);
		entityManager.flush();
		
		Schedule schedule = new Schedule();
		schedule.setRoute(Route.R11);
		schedule.setDate(DateUtil.getTimestampFromDDMMYYYY("24/03/2011", 0));
		schedule.setStatus(ScheduleStatus.unpriortized);
		
		POS pos = new POS();
			pos.setName("BTNC");
			pos.setLatitute(13.711428);
			pos.setLongtitute(100.594103);
			
		POS pos2 = new POS();
			pos2.setName("The Mall Ramkhamhaeng 3");
			pos2.setLatitute(13.752729);
			pos2.setLongtitute(100.609644);
			
		POS pos3 = new POS();
			pos3.setName("Seconsquare");
			pos3.setLatitute(13.695265);
			pos3.setLongtitute(100.648187);
			
		POS pos4 = new POS();
			pos4.setName("Central Department Store");
			pos4.setLatitute(13.669594);
			pos4.setLongtitute(100.634778);
			
		entityManager.persist(pos);
		entityManager.persist(pos2);
		entityManager.persist(pos3);
		entityManager.persist(pos4);
		entityManager.flush();
		
		Envelope e1 = new Envelope();
			e1.setEnvelopeType(EnvelopeType.delivered);
			e1.setEnvelopId("D001");
			e1.setNoOfProduct(10);
		Envelope e2 = new Envelope();
			e2.setEnvelopeType(EnvelopeType.delivered);
			e2.setEnvelopId("D002");
			e2.setNoOfProduct(5);
		Envelope e3 = new Envelope();
			e3.setEnvelopeType(EnvelopeType.delivered);
			e3.setEnvelopId("D003");
			e3.setNoOfProduct(4);
		
		entityManager.persist(e1);
		entityManager.persist(e2);
		entityManager.persist(e3);
		entityManager.flush();
		
		Mission mission = new Mission();
//			mission.setFromPOS(pos);
			mission.setToPOS(pos2);
			mission.setPriority(1);
			mission.setDevType(DeliveryType.refill);
			mission.setStatus(MissionStatus.incomplete);
			mission.addEnvelopes(e1);
			
		Mission mission2 = new Mission();
//			mission2.setFromPOS(pos2);
			mission2.setToPOS(pos3);
			mission2.setPriority(2);
			mission2.setDevType(DeliveryType.refill);
			mission2.setStatus(MissionStatus.incomplete);
			mission2.addEnvelopes(e2);
			
		Mission mission3 = new Mission();
//			mission3.setFromPOS(pos3);
			mission3.setToPOS(pos4);
			mission3.setPriority(3);
			mission3.setDevType(DeliveryType.newProduct);
			mission3.setStatus(MissionStatus.incomplete);
			mission3.addEnvelopes(e3);
		
		schedule.addMission(mission);
		schedule.addMission(mission2);
		schedule.addMission(mission3);
		
		logistic.addSchedules(schedule);
		
		entityManager.persist(schedule);
		entityManager.flush();
		
		
		//another schedule
		Schedule tschedule = new Schedule();
		tschedule.setRoute(Route.R12);
		tschedule.setDate(DateUtil.getCurrentTimestamp());
		tschedule.setStatus(ScheduleStatus.priortized);
		
//		POS tpos = new POS();
//			tpos.setName("BTNC heuartead qr");
//			tpos.setLatitute(13.711428);
//			tpos.setLongtitute(100.594103);
			
		POS tpos2 = new POS();
			tpos2.setName("Central Pinklao");
			tpos2.setLatitute(13.777779);
			tpos2.setLongtitute(100.476534);
			
		POS tpos3 = new POS();
			tpos3.setName("Tung-Hau-Seng Thonburi");
			tpos3.setLatitute(13.787295);
			tpos3.setLongtitute(100.486796);
			
		POS tpos4 = new POS();
			tpos4.setName("Tung-Hua-Seng Banglumpu");
			tpos4.setLatitute(13.761176);
			tpos4.setLongtitute(100.497574);
		
		POS tpos5 = new POS();
			tpos5.setName("The Mall Bangkae");
			tpos5.setLatitute(13.714556);
			tpos5.setLongtitute(100.407947);
			
//		entityManager.persist(tpos);
		entityManager.persist(tpos2);
		entityManager.persist(tpos3);
		entityManager.persist(tpos4);
		entityManager.persist(tpos5);
		entityManager.flush();
		
		Envelope te1 = new Envelope();
			te1.setEnvelopeType(EnvelopeType.delivered);
			te1.setEnvelopId("D011");
			te1.setNoOfProduct(10);
		Envelope te2 = new Envelope();
			te2.setEnvelopeType(EnvelopeType.delivered);
			te2.setEnvelopId("D012");
			te2.setNoOfProduct(5);
		Envelope te4 = new Envelope();
			te4.setEnvelopeType(EnvelopeType.delivered);
			te4.setEnvelopId("D014");
			te4.setNoOfProduct(6);
		
		entityManager.persist(te1);
		entityManager.persist(te2);
		entityManager.persist(te4);
		entityManager.flush();
		
		Mission tmission = new Mission();
			tmission.setFromPOS(pos);
			tmission.setToPOS(tpos2);
			tmission.setPriority(1);
			tmission.setDevType(DeliveryType.refill);
			tmission.setStatus(MissionStatus.incomplete);
			tmission.addEnvelopes(te1);
			
		Mission tmission2 = new Mission();
			tmission2.setFromPOS(tpos2);
			tmission2.setToPOS(tpos3);
			tmission2.setPriority(2);
			tmission2.setDevType(DeliveryType.refill);
			tmission2.setStatus(MissionStatus.incomplete);
			tmission2.addEnvelopes(te2);
		  
		Mission tmission3 = new Mission();
			tmission3.setFromPOS(tpos3);
			tmission3.setToPOS(tpos4);
			tmission3.setPriority(3);
			tmission3.setDevType(DeliveryType.getBill);
			tmission3.setStatus(MissionStatus.incomplete);
			
		Mission tmission4 = new Mission();
			tmission4.setFromPOS(tpos3);
			tmission4.setToPOS(tpos5);
			tmission4.setPriority(4);
			tmission4.setDevType(DeliveryType.newProduct);
			tmission4.setStatus(MissionStatus.incomplete);
			tmission4.addEnvelopes(te4);
		
		tschedule.addMission(tmission);
		tschedule.addMission(tmission2);
		tschedule.addMission(tmission3);
		tschedule.addMission(tmission4);

		driver.addSchedules(tschedule);

		entityManager.persist(tschedule);
		entityManager.flush();
	}
}